package com.gitee.ice1938.mvc.base;

import net.datatables.ajax.FormData;
import net.datatables.ajax.PageImpl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;

/**
 * 
 * @author ice1938@qq.com
 *
 */
public abstract class PageConvert {

	public static Pageable convertPageable(FormData data) {
		if (data == null || data.getLength() == 0) {
			return null;
		}
		Pageable pagebale = PageRequest.of(data.getStart() / data.getLength(), data.getLength());
		return pagebale;

	}

	public static Order convertOrder(FormData data) {
		if (data == null) {
			return null;
		}
		Direction dir = Direction.ASC;
		for (Direction element : Direction.values()) {
			if (element.name().equalsIgnoreCase(data.getOrderDir())) {
				dir = element;
			}
		}
		if (data.getColumnsData() != null) {
			Order order = new Order(dir, data.getColumnsData());
			return order;
		}
		return null;
	}

	public static <T> PageImpl<T> convertPage(Page<T> data) {
		if (data == null) {
			return null;
		}
		PageImpl<T> page = new PageImpl<T>(data.getContent(), data.getTotalElements());
		return page;
	}
}

